//
//  HomeClickCountryView.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/4/25.
//

import UIKit

class HomeClickCountryView: UIView {
    
    var selectBtnTag:UIButton!
    let heightInterval:CGFloat = 45
    let widthInterval:CGFloat = 40
    
    typealias ClickCountryBlock = (_ indNum:Int) -> Void
    var clickCountryBlock :  ClickCountryBlock!
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        var interval:CGFloat!
        for index in 0...2 {
            let btn:UIButton = UIButton()
            btn.tag = 101+index
            btn.addTarget(self, action: #selector(clickCountryBtn(_:)), for: .touchUpInside)
            btn.setTitleColor(UIColor.init(hexString: "#565656"), for: .normal)
            btn.setTitleColor(UIColor.black, for: .selected)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            btn.frame = CGRect(x: CGFloat(index) * widthInterval,y: 0,width: widthInterval,height: heightInterval)
            if index == 0 {
                interval = 0;
                btn.setTitle("မြန်မာ", for: .normal)
                btn.setImage(UIImage.init(named: "login_Burma_flag"), for: .normal)
                btn.setImage(UIImage.init(named: "login_Burma_flag_select"), for: .selected)
            } else if index == 1 {
                interval = 8;
                btn.setTitle("EN", for: .normal)
                btn.setImage(UIImage.init(named: "login_USA_flag"), for: .normal)
                btn.setImage(UIImage.init(named: "login_USA_flag_select"), for: .selected)
            } else {
                interval = 8;
                btn.setTitle("中", for: .normal)
                btn.setImage(UIImage.init(named: "login_China_flag"), for: .normal)
                btn.setImage(UIImage.init(named: "login_China_flag_select"), for: .selected)
            }
            
            let imgW:CGFloat = btn.imageView!.frame.size.width
            let imgH:CGFloat = (btn.imageView?.frame.size.height)!
            let lblW:CGFloat = btn.titleLabel!.frame.size.width
            let lblH:CGFloat = btn.titleLabel!.frame.size.height
            //设置图片和文字的间距，这里可自行调整
            let margin:CGFloat = 0
            btn.titleEdgeInsets = UIEdgeInsets(top: imgH+margin/2, left:-imgW, bottom: 0, right: 0)
            
            btn.imageEdgeInsets = UIEdgeInsets(top: -lblH-margin/2, left:(widthInterval-imgW)/2 - interval, bottom: 0, right: -lblW)
            
            self.addSubview(btn)
        }
        let str = UserDefaults.standard.string(forKey:LanguageHeader.MySystemLanguage)
        var selectbtn:UIButton!
        if (str == LanguageHeader.ChinaLanguage) {
            selectbtn = self.viewWithTag(103) as? UIButton
        } else if (str == LanguageHeader.BurmaLanguage) {
            selectbtn = self.viewWithTag(102) as? UIButton
        } else {
            selectbtn = (self.viewWithTag(101) as! UIButton)
        }
        self.selectBtnTag = selectbtn;
        self.selectBtnTag.isSelected = true
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    @objc func clickCountryBtn(_ selectBtn:UIButton) {
        if self.selectBtnTag == selectBtn {
            return
        }
        self.selectBtnTag.isSelected = false
        self.selectBtnTag = selectBtn
        if selectBtn.tag == 101 {
            UserDefaults.standard.set(LanguageHeader.BurmaLanguage, forKey:LanguageHeader.MySystemLanguage)
        } else if selectBtn.tag == 102 {
            UserDefaults.standard.set(LanguageHeader.USALanguage, forKey:LanguageHeader.MySystemLanguage)
        } else {
            UserDefaults.standard.set(LanguageHeader.ChinaLanguage, forKey:LanguageHeader.MySystemLanguage)
        }
        self.selectBtnTag.isSelected = true
        NotificationCenter.default.post(name: Notification.Name("languageNotification"), object: self)
        
        clickCountryBlock(selectBtn.tag)
    }
}
